package cn.wolfcode.crud.web.controller;


import cn.wolfcode.crud.domain.SystemDictionary;
import cn.wolfcode.crud.domain.SystemDictionaryItem;
import cn.wolfcode.crud.qo.SystemDictionaryItemQuerObjcet;
import cn.wolfcode.crud.service.ISystemDictionaryItemService;
import cn.wolfcode.crud.qo.QuerObjcet;
import cn.wolfcode.crud.qo.JsonResult;
import cn.wolfcode.crud.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequiresPermissions(value = {"systemDictionaryItem:list","字典明细页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQuerObjcet qo){
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        model.addAttribute("PageResult", pageInfo);
        List<SystemDictionary> systemDictionaries = systemDictionaryService.listAll();
        model.addAttribute("systemDictionaries", systemDictionaries);
        return "systemDictionaryItem/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"systemDictionaryItem:delete","字典明细删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            systemDictionaryItemService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"systemDictionaryItem:saveOrUpdate","字典明细新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){
        if (systemDictionaryItem.getSequence()==null){
            Integer[] sequences= systemDictionaryItemService.selecttss(systemDictionaryItem.getParentId());
           //记得数据库的数据不能有空
            if (sequences!=null){
                Integer max =sequences[0].intValue();//打第一个为最大值
                for (int i =0;i<sequences.length;i++){
                    if (sequences[i].intValue()>max.intValue()){
                        max=sequences[i].intValue();
                    }
                }
                systemDictionaryItem.setSequence(max+1);
            }

        }
        if (systemDictionaryItem.getId() != null) {
            systemDictionaryItemService.update(systemDictionaryItem);
        }else {
            systemDictionaryItemService.save(systemDictionaryItem);
        }
        return new JsonResult();
    }
}
